<?php 

class QpidBalanceDataProvider {
	
	public static function perFirstOrder($owner_id) {
		$arr = array(
			'pagination' => array('pageSize' => 100),
			'totalItemCount' => 100
		);
		$total_commision = 0;
		$sql = "SELECT
					h.adddate,
					h.website,
					h.website_id,
					year(adddate) year,
					month(adddate) month,					
					sum(h.amount) amount
				FROM first_order f, order_history h
				WHERE f.order_id=h.order_id
				AND owner_id='$owner_id'
				AND f.payment_status=0
				AND h.verify_status=1
				GROUP BY year(h.adddate) + month(h.adddate), website_id
				ORDER BY adddate DESC";
		$dataProvider = new QpidSqlDataProvider($sql, $arr);

		$sql = "SELECT 
					website,
					website_id,
					count(*) num,
					year(date_join) year ,
					month(date_join) month
				FROM user 
				WHERE owner_id='$owner_id' 
				AND payment_status=0 
				AND accept='Y' 
				GROUP BY year(date_join) + month(date_join), website_id";
		$data = Yii::app()->db->createCommand($sql)->queryAll();
		
		$new_data = $dataProvider->data;
		$total_data = array('website' => 'Total'); //用于计算总的佣金金额
		
		foreach ($new_data as $k => $item) {
			$new_data[$k]->commission = $item->amount * Yii::app()->params['paymentRates']['perFirstOrder']['order'];
			foreach ($data as $i => $v) {
				if ($item->year.$item->month == $v['year'].$v['month'] && $item->website_id == $v['website_id']) {
					$new_data[$k]->commission += $v['num'] * Yii::app()->params['paymentRates']['perFirstOrder']['lead'];
					$members -= $v['num'];		
					$total_commision += $new_data[$k]->commission;						           
				}
			}
			$total_data['commission'] += $new_data[$k]->commission;
		}
		array_push($new_data, (object)$total_data);
		$dataProvider->setData($new_data);
		
		return $dataProvider;
	}
	
	public static function perLead($owner_id) {
		$members = User::getLeads($owner_id);
		
		$arr = array(
			'pagination' => array('pageSize' => 100),
			'totalItemCount' => 100
		);
		$sql = "SELECT 
					website,
					website_id,
					count(*) num,
					year(date_join) year ,
					month(date_join) month
				FROM user 
				WHERE owner_id='$owner_id' 
				AND payment_status=0 
				AND accept='Y' 
				GROUP BY year(date_join) + month(date_join), website_id";
		$dataProvider = new QpidSqlDataProvider($sql, $arr);
		$data = $dataProvider->data;
		$total_data = array('website' => 'Total', 'commission' => 0); //用于计算总的佣金金额
		foreach ($data as $k => $item) {
			$data[$k]->commission = self::countCommission($item->num, $members);
			$total_data['commission'] += $data[$k]->commission;
			$members -= $item->num;
		}
		array_push($data, (object)$total_data);
		$dataProvider->setData($data);
		return $dataProvider;
	}
	
	/**
	 * 计算加盟账号所赚取的佣金
	 * @param int $count
	 */
	protected static function countCommission($count, $total) {
		$conf = Yii::app()->params['paymentRates']['perLead'];
		$amount = 0;
		for ($i=0; $i<$count && $total>0; $i++, $total--) {
			if ($total >= $conf[0][1] && $total <= $conf[0][2]) {
				$amount += $conf[0][0];
			} else if ($total >= $conf[1][1] && $total <= $conf[1][2]) {
				$amount += $conf[1][0];
			} else if ($total >= $conf[2][1]) {
				$amount += $conf[2][0];
			}
		}
		return $amount;
	}
}
